<?php
session_start();
class Comment {
	
	function get_comments_by_place_id($place_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$votes = 0;
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT user_id, username, user_photo_file, comment_id, comment_date_time, comment_body FROM comments NATURAL LEFT JOIN users WHERE place_id = '$place_id' ORDER BY comment_date_time DESC";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					if($user_photo_file == null) {
						$user_photo_file = "default-photo.jpg";
					} else {
						$user_photo_file = "profile/$user_photo_file";
					}
					$sql2 = "SELECT SUM(vote) AS votes_sum FROM comment_vote WHERE comment_id = '$comment_id'";
					if($result2 = $mysqli->query($sql2)) {
						while($row2 = $result2->fetch_array(MYSQLI_ASSOC)) {
							extract($row2);
							if($votes_sum != '')
								$votes = $votes_sum;
							else
								$votes = 0;
						}
					}
					$result2->close();
					$each = array(
						"user_id" => "$user_id",
						"username" => "$username",
						"user_photo" => "$user_photo_file",
						"comment_id" => "$comment_id",
						"date_time" => "$comment_date_time",
						"body" => "$comment_body",
						"votes" => "$votes",
					);
					$list[] = $each;
				}
				return $list;
			} else {
				return 2;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	function submit_comment($user_id, $place_id, $date_time, $body) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "INSERT INTO comments(user_id, place_id, comment_date_time, comment_body) VALUES('$user_id', '$place_id', '$date_time', '$body')";
		$mysqli->query($sql);
		$comment_id = $mysqli->insert_id;
		
		$sql = "SELECT username, user_photo_file FROM users WHERE user_id = '$user_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					if($user_photo_file == null) {
						$user_photo_file = "default-photo.jpg";
					} else {
						$user_photo_file = "profile/$user_photo_file";
					}
					$each = array(
						"comment_id" => "$comment_id",
						"user_id" => "$user_id",
						"username" => "$username",
						"user_photo" => "$user_photo_file",
						"date_time" => "$date_time",
						"body" => "$body",
					);
				}
				return $each;
			} else {
				return 2;
			}
			$result->close();
		}
		$mysqli->close();
		return $array;
	}
	
	function vote($comment_id, $user_id, $new_vote) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		$sql = "SELECT vote, vote_id FROM comment_vote WHERE comment_id = '$comment_id' AND user_id = '$user_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					if($vote == $new_vote) {
						return 0;
					} else if($vote != $new_vote) {
						$sql = "DELETE FROM comment_vote WHERE vote_id = '$vote_id'";
						$mysqli->query($sql);
						$sql = "INSERT INTO comment_vote(comment_id, user_id, vote) VALUES('$comment_id', '$user_id', '$new_vote')";
						$mysqli->query($sql);
						return 3;
					}
				}
			} else {
				$sql = "INSERT INTO comment_vote(comment_id, user_id, vote) VALUES('$comment_id', '$user_id', '$new_vote')";
				$mysqli->query($sql);
				return 1;
			}
			//solve this entire vote class.
			//The vote = 1 means upvote
			//The vote = 0 means downvote
			$result->close();
		} else {
			return 0;
		}
		/*
		if($vote == 1) {
			return 1;
		} else if($vote == 0) {
			//downvote
			return 0;
		}
		*/
		$mysqli->close();
	}
	
	function delete_comment($comment_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		
		$sql = "DELETE FROM comments WHERE comment_id = '$comment_id'";
		$mysqli->query($sql);
		$mysqli->close();
	}
}
?>